package com.sky.service.impl;

import com.atom.module.common.BaseContext;
import com.atom.module.common.result.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.sky.common.constant.MessageConstant;
import com.sky.common.constant.PasswordConstant;
import com.sky.common.constant.StatusConstant;
import com.sky.common.exception.AccountLockedException;
import com.sky.common.exception.AccountNotFoundException;
import com.sky.common.exception.PasswordErrorException;
import com.sky.pojo.dto.EmployeeDTO;
import com.sky.pojo.dto.EmployeeLoginDTO;
import com.sky.pojo.dto.EmployeePageQueryDTO;
import com.sky.pojo.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {

        // 获取账号,密码
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        Employee employee = employeeMapper.getByUsername(username);

        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        log.info(" md5 {}", password);
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    @Override
    public Long addEmp(EmployeeDTO dto) {
        // 进行转换成数据库使用的Bean
        Employee employee = new Employee();
        // 属性拷贝
        BeanUtils.copyProperties(dto, employee);

        // 更新字段
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employee.setStatus(StatusConstant.ENABLE);

        // 使用公共字段自动填充逻辑
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        // 从ThreadLocal中获取出登陆人id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());

        // 进行数据库插入
        employeeMapper.insert(employee);

        // 返回插入的Id
        return employee.getId();
    }

    @Override
    public Boolean deleteEmp(Long id) {
        return employeeMapper.deleteEmpById(id);
    }

    @Override
    public Boolean deleteEmp(String username) {
        return employeeMapper.deleteEmpByUserName(username);
    }

    @Override
    public PageResult page(EmployeePageQueryDTO dto) {
        // 1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        // 2.调用mapper的查询方法,并强转返回类型为Page
        Page<Employee> page = employeeMapper.list(dto.getName());

        // 3.封装PageResult对象并返回
        return new PageResult(page.getResult(), page.getTotal());
    }

    @Override
    public Boolean enableOrDisable(Integer status, Long id) {
        Employee employee = Employee.builder().id(id).status(status)
//                .updateTime(LocalDateTime.now()).updateUser(BaseContext.getCurrentId())
                .build();
        return employeeMapper.update(employee);
    }

    @Override
    public Employee getById(Long id) {
        return employeeMapper.getById(id);
    }

    @Override
    public void update(EmployeeDTO dto) {
        Employee employee = new Employee();
        // 拷贝属性值
        BeanUtils.copyProperties(dto, employee);
        // 补充更新时间和更新人
        // 使用公共字段自动填充逻辑
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.update(employee);
    }
}
